package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.fire.matterManagement.dto.MemorandumDto;
import com.dykj.fireFighting.api.fire.matterManagement.entity.Memorandum;
import com.dykj.fireFighting.api.fire.matterManagement.service.IMemorandumService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

/**
 * @Author: zly
 * @Description : app备忘录
 * @Date: 2021-09-01
 */
@Slf4j
@Api(value = "维保app 备忘录接口", tags = {"维保app 备忘录接口"})
@RestController
@RequestMapping("app/memorandum")
public class AppMemorandumController {

    @DubboReference
    private IMemorandumService memorandumService;


    /**
     * 维保app 备忘录列表
     *
     * @return
     */
    @AutoLog(value = "维保app 备忘录-分页列表查询")
    @ApiOperation(value = "维保app 备忘录分页列表", notes = "维保app 备忘录分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<Memorandum>> treeGrid(MemorandumDto memorandumDto) {

        memorandumDto.setUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<Memorandum> page = memorandumService.queryList(memorandumDto);
        return CommonResult.success(page);
    }

    /**
     * 获取维保app 备忘录信息
     */
    @AutoLog(value = "维保app 备忘录-获取维保app 备忘录信息")
    @ApiOperation(value = "获取维保app 备忘录信息", notes = "获取维保app 备忘录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<Memorandum> info(@PathVariable("id") String id){
        Memorandum memorandum = memorandumService.getById(id);
        return CommonResult.success(memorandum);
    }

    /**
     * 保存维保app 备忘录信息
     */
    @AutoLog(value = "维保app 备忘录-保存维保app 备忘录信息")
    @ApiOperation(value = "保存维保app 备忘录信息", notes = "保存维保app 备忘录信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody Memorandum memorandum) {
        try {
            memorandum.setId(UUIDUtils.get32UUID());
            LoginUserVo user = ShiroUtil.getUser();
            memorandum.setCreatBy(user.getId().toString());
            memorandum.setCreateByName(user.getLoginName());
            memorandum.setCreateDate(new Date());
            memorandum.setUnitId(user.getEnterpriseId().toString());
            memorandumService.save(memorandum);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改维保app 备忘录信息
     */
    @AutoLog(value = "维保app 备忘录-修改维保app 备忘录信息")
    @ApiOperation(value = "修改维保app 备忘录信息", notes = "修改维保app 备忘录信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody Memorandum memorandum) {
        try {

            memorandumService.updateById(memorandum);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除维保app 备忘录信息
     */
    @AutoLog(value = "维保app 备忘录-删除维保app 备忘录信息")
    @ApiOperation(value = "删除维保app 备忘录信息", notes = "删除维保app 备忘录信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            memorandumService.deleteByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
